//
//  ST_BTWebViewController.swift
//  DrinkTogether
//
//  Created by DouLei on 2018/4/4.
//  Copyright © 2018 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import WebKit
import RxCocoa
import RxSwift


class ST_BTWebViewController: UIViewController,  WKNavigationDelegate {
    
    var webView = WKWebView()
    
    var urlStr:String = ""
    
    var titleStr = ""
    
    /// 返回上一个页面监听
    let goback_publisher = PublishSubject<Void>()
    
    deinit{
        print("webView did deinit tag 09090")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = Color.white
        webView.backgroundColor = Color.clear
        webView.isOpaque = false
        webView.navigationDelegate = self
        
        set_up_view()
        self.view.addSubview(webView)
        let line = UIView()
        line.backgroundColor = Color.init(hex: 0x999999, transparency: 0.3)
        self.view.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.top.equalTo(back_btn.snp.bottom).offset(5)
            make.left.right.equalTo(self.view)
            make.height.equalTo(1)
        }
        webView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(line.snp.bottom)
        }
        

        
        
        if let url = URL.init(string: urlStr){
            webView.load(URLRequest(url: url))
        }
        
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    func set_up_view(){
   
        self.view.addSubview(navi_view)
        self.view.addSubview(back_btn)
        self.view.addSubview(titleLabel)
        
        
        navi_view.snp.makeConstraints { (make ) in
            make.left.right.top.equalTo(self.view)
            
            make.height.equalTo(navigationBarHeight + statusBarHeight)
        }
        
        back_btn.snp.makeConstraints { (make ) in
            make.top.equalTo(self.view).offset(statusBarHeight)
            make.left.equalTo(self.view).offset(8)
            make.width.height.equalTo(44)
        }
        titleLabel.snp.makeConstraints { (make ) in
            make.centerY.equalTo(back_btn)
            make.centerX.equalTo(self.view)
            make.height.equalTo(25)
        }
    }
    lazy var navi_view : UIView = {
        let view = UIView()
        view.backgroundColor = Color.white
        return view
    }()
    lazy var back_btn:UIButton = {
        let btn = UIButton()
        
        btn.setImageForAllStates(ST_ImageUtils.imageName("返回_gray")!)
        btn.addTarget(self, action: #selector(back_action(sender: )), for: .touchUpInside)
        return btn
    }()
    
    @objc func back_action(sender: UIButton) {
        
        goback_previous_controller(animated: true) {
            self.goback_publisher.onNext(())
        }
    }
    
    lazy var titleLabel:UILabel = {
        let control = UILabel()
        
        control.textColor = Color.black
        control.font = UIFont.systemFont(ofSize: 18)
        
        control.text = titleStr
        return control
    }()

    
    // MARK: - delegate
//    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//
//        if let title = webView.title{
//            self.titleLabel.text = title
//        }
//
//    }
}
